/*
 * ======================================================================
 *
 *       Filename:  path.c
 *
 *
 *        Created:  10/04/2011 04:30:36 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  durand_y, <durand.yoan@gmail.com>
 *
 * =======================================================================
 */
#include "find.h"
#include <stdio.h>

/* Exclude unwanted file */
static int          test(char           *fname)
{
    if ((mystrcmp(fname, "..") == 1) || (mystrcmp(fname, ".") == 1))
        return 1;
    else
        return 0;
}

static int          check_last(char     *str)
{
    if (str[mystrlen(str) - 1] == '/')
        return (1);
    else
        return (0);
}

int          permission(char *path)
{
    DIR             *dir = NULL;
    int             fd = 0;

    if ((dir = opendir(path)) == NULL)
    {
            if ((fd = (open(path, O_RDONLY))) == -1)
                return 0;
            else
            {
                    close(fd);
                    return (1);
            }
    }
    else
    {
        closedir(dir);
        return 1;
    }
}

void                my_error(char      *bin,
                             char      *error)
{

    if(error[0] != '-')
    {
        bin = mystrcat(bin, error);
        bin = mystrcat(bin, "'");
        perror(bin);
    }
    else
    {
        bin = mystrcat(bin,": unknown predicate `");
        bin = mystrcat(bin, error);
        bin = mystrcat(bin, "'\n");
        write(STDOUT_FILENO, bin, mystrlen(bin));
    }
    exit (1);
}

void                my_error2(char       *bin,
                             char       *err,
                             int        type)
{
    bin = mystrcat(bin, ": ");
    if (type == 1)
    {
        bin = mystrcat(bin, "missing argument to `");
        bin = mystrcat(bin, err);
        bin = mystrcat(bin, "'\n");
    }
    else if (type == 2)
       {
           err = mystrcat (err, "\n");
            bin = mystrcat(bin, "paths must precede expression: ");
            bin = mystrcat(bin, err);
       }
    write(STDOUT_FILENO, bin, mystrlen(bin));
}
static s_list       *rec(struct dirent  *file,
                        s_list          *list,
                        char            *path)
{
    char            *name = NULL;
    struct stat     buf;

    if (test(file->d_name) == 0)
    {
        name = mystrcat(path, file->d_name);
        stat(name, &buf);
        if(S_ISDIR(buf.st_mode))
            listdir(name, list);
        else
            list = add(list, name);
    }
    return (list);
}

void                listdir(char        *path,
                            s_list      *list)
{
    DIR             *dir;
    struct dirent   *file;

       list = add(list, path);
       if ((dir = opendir(path)) != NULL)
        {
            if (check_last(path) == 0)
                path = mystrcat(path,"/");
            while((file = readdir(dir)) != NULL)
            {
                list = rec(file, list, path);
            }
            closedir(dir);
        }

}


